package com.eml.service.dao.impl;

import com.eml.service.dao.DelivererDao;
import com.eml.service.entity.Deliverer;
import com.eml.service.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DeliverersDaoImpl implements DelivererDao {


//配送员注册
    @Override
    public void delivererRegister(Deliverer deliverer) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql="insert into deliverer(Deliverer_name,Deliverer_username,Deliverer_password)values(?,?,?)";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,deliverer.getDelivererName());
        psmt.setString(2,deliverer.getDelivererUsername());
        psmt.setString(3,deliverer.getDelivererPassword());
        psmt.executeUpdate();

        JDBCUtils.close(conn,psmt);
    }
//配送员登录
    @Override
    public Deliverer delivererLogin(String delivererUsername) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql=
                "select Deliverer_id,Deliverer_name,Deliverer_username,Deliverer_password from deliverer where deliverer_username=?";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,delivererUsername);
        ResultSet rs = psmt.executeQuery();
        if (rs.next()) {
            int delivererId = rs.getInt("Deliverer_id");
            String delivererName=rs.getString("Deliverer_name");
            String name = rs.getString("Deliverer_username");
            String password = rs.getString("Deliverer_password");
            JDBCUtils.close(conn,psmt,rs);
            return new Deliverer(delivererId,delivererName,name,password);
        }
        JDBCUtils.close(conn,psmt,rs);
        return null;
    }
//修改密码
    @Override
    public int updateDelivererPassword(String delivererUsername, String delivererPassword) throws Exception {
        Connection conn = JDBCUtils.getConnection();
        String sql="update deliverer set deliverer_password=? where deliverer_username=?";
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,delivererPassword);
        psmt.setString(2,delivererUsername);
        return psmt.executeUpdate();
    }

    @Override
    public void updateOrder() throws Exception {

    }

    @Override
    public void selectOrderInfo() throws Exception {

    }

    @Override
    public void provideFeedbackFaq() throws Exception {

    }
}
